package com.dongdongshop.controller;

import com.dongdongshop.data.Result;
import com.dongdongshop.enums.ResultEnums;
import com.dongdongshop.pojo.TbUser;
import com.dongdongshop.service.TbUserService;
import com.dongdongshop.uitl.ShiroUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.w3c.dom.stylesheets.LinkStyle;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;


@Controller
@RequestMapping("TbUser")
public class TbUserController {

    //注入service
    @DubboReference
    private TbUserService tbUserService;
    @Autowired
    private RedisTemplate redisTemplate;

    //登录;逻辑
    @ResponseBody
    @RequestMapping("login")
    public Result login(TbUser tbAdmin, HttpServletRequest request) {

        //使用shiro编写认证操作
        //获取Subject对象
        Subject subject = SecurityUtils.getSubject();
        //封装用户数据
        UsernamePasswordToken token = new UsernamePasswordToken(tbAdmin.getUsername(), tbAdmin.getPassword());
        //执行登录方法
        Result result = null;
        try {
            //登录成功
            subject.login(token);
            TbUser tbUser = (TbUser)SecurityUtils.getSubject().getPrincipal();
            redisTemplate.boundHashOps("user").put("user",tbUser);
            result = Result.response(ResultEnums.LOGIN_SUCCESS);
        } catch (UnknownAccountException e) {
            //用户名不存在
            result = Result.response(ResultEnums.LOGIN_ERRORNAME);
        } catch (IncorrectCredentialsException e) {
            result = Result.response(ResultEnums.LOGIN_ERRORPASSWORD);
        }
        return result;
    }

    //注册逻辑
    @ResponseBody
    @RequestMapping("register")
    public Result register(TbUser tbAdmin) {
        try {
            TbUser tbAdminByName = tbUserService.findTbAdminByName(tbAdmin.getUsername());
            if (tbAdminByName != null){
                return Result.response(ResultEnums.REGISTER_ERRORNAME);
            }
            tbAdmin.setCreated(new Date());
            tbAdmin.setUpdated(new Date());
            String qq = tbAdmin.getQq();
            String phone = tbAdmin.getPhone();
            String o = (String)redisTemplate.opsForValue().get(phone);
            if (!qq.equals(o)){
                return Result.response(ResultEnums.MESSAGE);
            }
            String s = ShiroUtils.generateSalt(6);
            tbAdmin.setSalt(s);
            //需要加密的密码
            String pwd = tbAdmin.getPassword();
            //加密方法
            String md5 = ShiroUtils.encryptPassword("MD5", pwd, s, 3);
            tbAdmin.setPassword(md5);
            tbUserService.add(tbAdmin);
            return Result.response(ResultEnums.REGISTER_SUCCESS);
        }catch (Exception e){
            System.out.println(e);
            return Result.response(ResultEnums.REGISTER_ERROR);
        }
    }

}
